//
//  TTRSSITool.m
//  iBeaconDemo
//
//  Created by cry on 17/1/11.
//  Copyright © 2017年 eGova. All rights reserved.
//

#import "TTRSSITool.h"
#import "TTReceiver.h"

@implementation TTRSSITool

/**
 * 根据rssi计算距离
 * @param A
 * @param n
 * @param rssi
 * @return
 */
//public static double getDistance(int A,double n,int rssi) {
//    int iRssi = Math.abs(rssi);
//    double power = (iRssi - A)/(10*n);
//    return Math.pow(10, power);
//}
//
+ (double)getDistanceWithA:(NSInteger)a n:(double)n rssi:(NSInteger)rssi{
    NSInteger iRssi = labs(rssi);
    double power = (iRssi - a) / (10 *n);
    return pow(10, power);
}

//public static ArrayList<Receiver> fiterReceiver(ArrayList<Receiver> rs,HashMap<String, Integer> readings){
//    ArrayList<Receiver> res = null;
//    Receiver r;
//    if(rs == null || readings == null){
//        return null;
//    }
//    res = new ArrayList<Receiver>();
//    if(readings != null && readings.size()>0){
//        Iterator it= readings.keySet().iterator();
//        while(it.hasNext()){
//            String key;
//            key = it.next().toString();
//            for(int i=0;i<rs.size();i++){
//                r = rs.get(i);
//                if(r.getID().equals(key)){
//                    res.add(r);
//                    break;
//                }
//            }
//        }
//    }
//    return res;
//}
+ (NSMutableArray<TTReceiver *> *)fiterReceiver:(NSMutableArray<TTReceiver *> *)rs readings:(NSMutableDictionary<NSString *, NSNumber *> *)readings{
    
    NSMutableArray<TTReceiver *> *res = [NSMutableArray array];
    TTReceiver *r = nil;
    if(rs == nil || readings == nil){
        return nil;
    }
    if (readings != nil && readings.count > 0 ){
        for (NSString *key in readings) {
            for (NSInteger i = 0; i < rs.count; i++) {
                r = rs[i];
                if ([r.keyID isEqualToString:key]) {
                    [res addObject:r];
                    break;
                }
            }
        }
    }
    return res;
}
//public static double getMinX(ArrayList<Receiver> receiver,HashMap<String, Integer> readings,HashMap<String,Double> rdMap){
//    double res = 0;
//    double x;
//    double d;
//    ArrayList<Double> list = new ArrayList<Double>();
//    Receiver r;
//    if(readings != null){
//        for(int i=0;i<receiver.size();i++){
//            x = receiver.get(i).getXPos();
//            d = rdMap.get(receiver.get(i).getID());
//            list.add(x-d);
//        }
//    }
//    if(list != null && list.size() > 0){
//        res = list.get(0);
//        for(int i=0;i>list.size();i++){
//            if(res>list.get(i)){
//                res = list.get(i);
//            }
//        }
//    }
//    return res;
//}
+ (double)getMinXWithReceivers:(NSMutableArray<TTReceiver *> *)receiver
                      readings:(NSMutableDictionary<NSString *, NSNumber *> *)readings
                         rdMap:(NSMutableDictionary<NSString *, NSNumber *> *)rdMap{
    
    double res = 0;
    double x;
    NSNumber *d = nil;
    NSMutableArray<NSNumber *> *list = [NSMutableArray array];
    if (readings != nil) {
        for (NSInteger i = 0; i < receiver.count; i++) {
            x = receiver[i].xPos;
            d = rdMap[receiver[i].keyID];
            [list addObject:@(x - [d doubleValue])];
        }
    }
    if (list != nil && list.count > 0) {
        res = list[0].doubleValue;
        for (int i = 0 ; i < list.count; i++) {
            if (res > list[i].doubleValue) {
                res = list[i].doubleValue;
            }
        }
    }
    return res;
}

//public static double getMinY(ArrayList<Receiver> receiver,HashMap<String, Integer> readings,HashMap<String,Double> rdMap){
//    double res = 0;
//    double y;
//    double d;
//    ArrayList<Double> list = new ArrayList<Double>();
//    if(receiver != null){
//        for(int i=0;i<receiver.size();i++){
//            y = receiver.get(i).getYPos();
//            d = rdMap.get(receiver.get(i).getID());
//            list.add(y-d);
//        }
//    }
//    if(list != null && list.size() > 0){
//        res = list.get(0);
//        for(int i=0;i<list.size();i++){
//            if(res>list.get(i)){
//                res = list.get(i);
//            }
//        }
//    }
//    return res;
//}
+ (double)getMinYWithReceivers:(NSMutableArray<TTReceiver *> *)receiver
                      readings:(NSMutableDictionary<NSString *, NSNumber *> *)readings
                         rdMap:(NSMutableDictionary<NSString *, NSNumber *> *)rdMap{
    
    double res = 0;
    double x;
    NSNumber *d = nil;
    NSMutableArray<NSNumber *> *list = [NSMutableArray array];
    if (readings != nil) {
        for (NSInteger i = 0; i < receiver.count; i++) {
            x = receiver[i].yPos;
            d = rdMap[receiver[i].keyID];
            [list addObject:@(x - [d doubleValue])];
        }
    }
    if (list != nil && list.count > 0) {
        res = list[0].doubleValue;
        for (int i = 0 ; i < list.count; i++) {
            if (res > list[i].doubleValue) {
                res = list[i].doubleValue;
            }
        }
    }
    return res;
}
//public static double getMaxX(ArrayList<Receiver> receiver,HashMap<String, Integer> readings,HashMap<String,Double> rdMap){
//    double res = 0;
//    double x;
//    double d;
//    ArrayList<Double> list = new ArrayList<Double>();
//    if(receiver != null){
//        for(int i=0;i<receiver.size();i++){
//            x = receiver.get(i).getXPos();
//            d = rdMap.get(receiver.get(i).getID());
//            list.add(x+d);
//        }
//    }
//    if(list != null && list.size() > 0){
//        res = list.get(0);
//        for(int i=0;i<list.size();i++){
//            if(res<list.get(i)){
//                res = list.get(i);
//            }
//        }
//    }
//    return res;
//}
+ (double)getMaxXWithReceivers:(NSMutableArray<TTReceiver *> *)receiver
                      readings:(NSMutableDictionary<NSString *, NSNumber *> *)readings
                         rdMap:(NSMutableDictionary<NSString *, NSNumber *> *)rdMap{
    
    double res = 0;
    double x;
    NSNumber *d = nil;
    NSMutableArray<NSNumber *> *list = [NSMutableArray array];
    if (readings != nil) {
        for (NSInteger i = 0; i < receiver.count; i++) {
            x = receiver[i].xPos;
            d = rdMap[receiver[i].keyID];
            [list addObject:@(x + [d doubleValue])];
        }
    }
    if (list != nil && list.count > 0) {
        res = list[0].doubleValue;
        for (int i = 0 ; i < list.count; i++) {
            if (res < list[i].doubleValue) {
                res = list[i].doubleValue;
            }
        }
    }
    return res;
}
//public static double getMaxY(ArrayList<Receiver> receiver,HashMap<String, Integer> readings,HashMap<String,Double> rdMap){
//    double res = 0;
//    double y;
//    double d;
//    ArrayList<Double> list = new ArrayList<Double>();
//    if(receiver != null){
//        for(int i=0;i<receiver.size();i++){
//            y = receiver.get(i).getYPos();
//            d = rdMap.get(receiver.get(i).getID());
//            list.add(y+d);
//        }
//    }
//    if(list != null && list.size() > 0){
//        res = list.get(0);
//        for(int i=0;i<list.size();i++){
//            if(res<list.get(i)){
//                res = list.get(i);
//            }
//        }
//    }
//    return res;
//}
+ (double)getMaxYWithReceivers:(NSMutableArray<TTReceiver *> *)receiver
                      readings:(NSMutableDictionary<NSString *, NSNumber *> *)readings
                         rdMap:(NSMutableDictionary<NSString *, NSNumber *> *)rdMap{
    
    double res = 0;
    double x;
    NSNumber *d = nil;
    NSMutableArray<NSNumber *> *list = [NSMutableArray array];
    if (readings != nil) {
        for (NSInteger i = 0; i < receiver.count; i++) {
            x = receiver[i].yPos;
            d = rdMap[receiver[i].keyID];
            [list addObject:@(x + d.doubleValue)];
        }
    }
    if (list != nil && list.count > 0) {
        res = list[0].doubleValue;
        for (int i = 0 ; i < list.count; i++) {
            if (res < list[i].doubleValue) {
                res = list[i].doubleValue;
            }
        }
    }
    return res;
}
@end
